/**
 * Copyright by 山西大昌集团 (c) 2018 DaChang Inc.
 * Date:2018年9月7日上午10:24:05
 * @author : LuNanTing
 * @Description : TODO
 */
package com.hikvision.cms.api;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.google.protobuf.ByteString;
import com.hikvision.cms.api.eps.beds.EventDis;
import com.hikvision.cms.api.eps.beds.PmsEvent;
import com.hikvision.cms.api.object.entity.ParseEntity;
import com.hikvision.cms.api.object.entity.PmsEventEntity;
import com.hikvision.cms.api.util.OctToString;

/**
 * <p>
 * 此Demo提供的是接收事件代码样例，调用接收事件代码前请先 调用[事件订阅]接口
 * </p>
 * <p>
 * 接收入场事件demo
 * </p>
 *
 * @author : LuNanTing
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2017年10月10日
 * @since
 */
public class MainTest {

	/**
	 * 10.33.47.176:61618和openapi.vss.topic可通过openapi【事件订阅接口】获得
	 */
	public static final String BROKER_URL = "tcp://192.188.8.44:61616";

	/**
	 * 10.33.47.176:61618和openapi.vss.topic可通过openapi【事件订阅接口】获得
	 */
	public static final String TARGET = "openapi.pms.topic";

	public static void run() throws Exception {

		Connection connection = null;
		Session session = null;
		try {
			// 创建链接工厂
			ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
			// 通过工厂创建一个连接
			connection = factory.createConnection();
			// factory.createConnection(userName, password)
			// 启动连接
			connection.start();
			// 第一个参数表示是否使用事务，第二个参数指定消息的确认模式
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic(TARGET);
			MessageConsumer consumer = session.createConsumer(topic);
			// 消费者异步接收topic里的消息
			consumer.setMessageListener(new MessageListener() {

				public void onMessage(Message msg) {
					try {
						// cms里发送的消息为BytesMessage，此处不做判断亦可
						if (msg instanceof BytesMessage) {
							BytesMessage bytesMessage = (BytesMessage) msg;
							long length = bytesMessage.getBodyLength();
							byte[] bt = new byte[(int) length];
							// 将BytesMessage转换为byte类型
							bytesMessage.readBytes(bt);
							// 壳文件字段，EventDis类为event_dis.proto文件解析而来，CommEventLog类为事件壳文件类
							EventDis.CommEventLog parseFrom = EventDis.CommEventLog.parseFrom(bt);
							ParseEntity parseEntity = OctToString.OctStrtoStringForCommEvent(parseFrom);
							// 输出壳文件字段
							// 扩展字段，此字段为设备上报事件内容，部分事件需要使用pb文件再次解析
							ByteString extInfo = parseFrom.getExtInfo();
							// 输出扩展字段
							// 扩展字段解析
							PmsEvent.MsgPmsEvent parseFrom2 = PmsEvent.MsgPmsEvent.parseFrom(extInfo);

							PmsEventEntity pmsEventEntity = OctToString.OctStrtoStringForPmsEvent(parseFrom2);
							parseEntity.setExtInfo(pmsEventEntity);
							System.out.println("-------------------------解析字段-------------------------------");
							System.out.println(parseEntity);

						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 测试主方法
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		MainTest.run();
	}
}
